﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MappitLib {
    public partial class LayersControl : UserControl {
        //*********************************************************************
        #region Members
        private TilemapEditor tilemap_editor_ = null;
        private List<LayerBrowserItem> items_ = new List<LayerBrowserItem>();
        #endregion
        //*********************************************************************
        #region Properties
        public TilemapEditor TilemapEditor {
            get { return tilemap_editor_; }
            set {
                tilemap_editor_ = value;
                ResetItems();
            }
        }
        #endregion
        //*********************************************************************
        #region Methods
        public LayersControl() {
            InitializeComponent();
        }
        private void ResetItems() {
            if (TilemapEditor == null) {
                return;
            }
            int control_count = items_.Count;
            int layer_count = TilemapEditor.Layers.Count;
            // are there enough list items for all the layers?
            if (control_count < layer_count) {
                PadListItems(control_count, layer_count);
            } // are there too many?
            else if(control_count > layer_count) {
                TrimListItems(control_count, layer_count);
            }
            // iterate through layers
            for (int i = 0; i < TilemapEditor.Layers.Count; ++i) {
                LayerBrowserItem item = items_[i];
                item.SetLayer(TilemapEditor.Layers[i]);
                if (item.Layer == TilemapEditor.ActiveLayer) {
                    SelectItem(item);
                }
            }
        }

        private void TrimListItems(int control_count, int layer_count) {
            // trim the extra list items
            for (int i = 0; i < control_count - layer_count; ++i) {
                LayerBrowserItem item = items_.Last();
                items_.Remove(item);
                item.Dispose();
            }
        }

        private void PadListItems(int control_count, int layer_count) {
            for (int i = 0; i < layer_count - control_count; ++i) {
                CreateListItem();
            }
        }

        private void CreateListItem() {
            // create new item
            LayerBrowserItem new_item = new LayerBrowserItem(this);
            // determine the location for the new item
            if (items_.Count > 0) {
                LayerBrowserItem last_item = items_.Last();
                new_item.Location = new Point(0, last_item.Bottom + 1);
            }
            // add the new item
            items_.Add(new_item);
            // set click event handler for new item
            new_item.Click += new EventHandler(LayerBrowserItem_Click);
            // show the new item
            new_item.Show();
        }
        private void SelectItem(LayerBrowserItem selected_item) {
            selected_item.Focus();
            for (int i = 0; i < itemsPanel.Controls.Count; ++i) {
                LayerBrowserItem item = (LayerBrowserItem)itemsPanel.Controls[i];
                if (item == selected_item) {
                    item.IsSelected = true;
                } else {
                    item.IsSelected = false;
                }
            }
        }
        #endregion
        //*********************************************************************
        #region Event handlers
        void LayerBrowserItem_Click(object sender, EventArgs e) {
            LayerBrowserItem clicked_item = ((LayerBrowserItem)sender);
            SelectItem(clicked_item);
        }
        private void toolStripAddLayer_Click(object sender, EventArgs e) {
            Layer new_layer = TilemapEditor.AddLayer();
            ResetItems();
        }
        private void toolStripRemoveLayer_Click(object sender, EventArgs e) {
            TilemapEditor.RemoveLayer();
            ResetItems();
        }
    }
        #endregion

}
